﻿
using SvGame.Objects;

namespace SvGame.Controller;

public partial class GameHandler
{
    /// <summary>
    /// 装备
    /// </summary>
    /// <param name="pack"></param>
    public int C2GEquips(MyPackage pack)
    {
        pack.GetMsg(out C2GEquips req);
        var rsp = new G2CEquips();

        rsp.Tag = req.Tag;
        if (!pack.PackGetPlayer(out var player) || player is null)
        {
            return 0;
        }
        rsp.InfoEquips = player.InfoEquips();

        return pack.Send(rsp);
    }
    /// <summary>
    /// 装备拾取
    /// </summary>
    /// <param name="pack"></param>
    public int C2GEquipGet(MyPackage pack)
    {
        pack.GetMsg(out C2GEquipGet req);
        var rsp = new G2CEquipGet();

        if (!pack.PackGetPlayer(out var player) || player is null)
        {
            return 0;
        }
        if (player.IsBusy) 
        { 
            return player.MessageError(BusyMessage);
        }
        if (!player.Room.DictEquip.TryGetValue(req.Uid, out var obj))
        {
            return player.MessageError("没有这个装备");
        }
        player.EquipGetFromRoom(obj);

        MessageVision($"$N捡起{obj.DescShort()}。", player);

        player.Room.BroadcastInfo();
        player.NtfPlayerEquips();

        return pack.Send(rsp);
    }
    /// <summary>
    /// 装备丢弃
    /// </summary>
    /// <param name="pack"></param>
    public int C2GEquipDrop(MyPackage pack)
    {
        pack.GetMsg(out C2GEquipDrop req);
        var rsp = new G2CEquipDrop();

        rsp.Uid = req.Uid;
        if (!pack.PackGetPlayer(out var player) || player is null)
        {
            return 0;
        }
        if (player.IsBusy)
        {
            return player.MessageError(BusyMessage);
        }
        if (!player.DictEquip.TryGetValue(req.Uid, out var obj))
        {
            return player.MessageError("没有这个装备");
        }
        if (!ConfEquip.TryGetValue(obj.ConfigId, out var conf))
        {
            return player.MessageError("配置ID错误");
        }
        player.EquipDropToRoom(obj);

        MessageVision($"$N丢下{obj.DescShort()}。", player);

        player.Room.BroadcastInfo();

        player.EquipFinish();

        player.NtfPlayerEquips();

        return pack.Send(rsp);
    }
    /// <summary>
    /// 装备穿上
    /// </summary>
    /// <param name="pack"></param>
    public int C2GEquipWear(MyPackage pack)
    {
        pack.GetMsg(out C2GEquipWear req);
        var rsp = new G2CEquipWear();

        rsp.Uids.Add(req.Uid);
        if (!pack.PackGetPlayer(out var player) || player is null)
        {
            return 0;
        }
        if (player.IsBusy)
        {
            return player.MessageError(BusyMessage);
        }
        if (!player.DictEquip.TryGetValue(req.Uid, out var obj))
        {
            return player.MessageError("你身上没有这样东西");
        }
        if (!ConfEquip.TryGetValue(obj.ConfigId, out var conf))
        {
            return player.MessageError("配置ID错误");
        }
        if (player.DictWear.ContainsValue(obj.Uid))
        {
            return player.MessageError("已经穿上了");
        }

        var type = (EEquipType)obj.Cfg.EquipType;
        if (player.DictWear.TryGetValue(type, out var oldUid))
        {
            rsp.Uids.Add(oldUid);
        }

        player.EquipUnwear(type, true);
        player.EquipWear(obj, true);

        player.EquipFinish();

        player.NtfPlayerEquips();

        return pack.Send(rsp);
    }
    /// <summary>
    /// 装备脱下
    /// </summary>
    /// <param name="pack"></param>
    public int C2GEquipOff(MyPackage pack)
    {
        pack.GetMsg(out C2GEquipOff req);
        var rsp = new G2CEquipOff();

        if (!pack.PackGetPlayer(out var player) || player is null)
        {
            return 0;
        }
        if (player.IsBusy)
        {
            return player.MessageError(BusyMessage);
        }
        if (!player.DictEquip.TryGetValue(req.Uid, out var obj))
        {
            return player.MessageError("你身上没有这样东西");
        }
        if (!ConfEquip.TryGetValue(obj.ConfigId, out var conf))
        {
            return player.MessageError("配置ID错误");
        }
        if (!player.DictWear.ContainsValue(obj.Uid))
        {
            return player.MessageError("你并没有装备这样东西");
        }

        rsp.Uid = req.Uid;
        player.EquipUnwear((EEquipType)obj.Cfg.EquipType, true);

        player.EquipFinish();

        player.NtfPlayerEquips();

        return pack.Send(rsp);
    }
    /// <summary>
    /// 装备更换
    /// </summary>
    /// <param name="pack"></param>
    public int C2GEquipOutfit(MyPackage pack)
    {
        pack.GetMsg(out C2GEquipOutfit req);
        var rsp = new G2CEquipOutfit();

        if (!pack.PackGetPlayer(out var player) || player is null)
        {
            return 0;
        }

        var type = (EEquipType)req.Type;
        // 没变换不处理
        player.DictWear.TryGetValue(type, out var wearedUid);
        if (wearedUid == req.Uid)
        {
            return pack.Send(rsp);
        }

        if (player.IsBusy)
        {
            return player.MessageError(BusyMessage);
        }

        player.EquipUnwear(type, true);
        if (player.DictEquip.TryGetValue(req.Uid, out var obj))
        {
            player.EquipWear(obj, true);
        }

        player.EquipFinish();

        player.NtfPlayerEquips();

        return pack.Send(rsp);
    }
}
